<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript">
	function p_click(m) {
		var st = document.getElementsByClassName("per");
		var divst = document.getElementsByClassName("per_sub_content");
		for (var k = 0; k < st.length; k++) {
			if (k != m) {
				divst[k].style.display = "none";
			} else {
				divst[k].style.display = "block";
			}
		}
	}
</script>

<div class="personal content">
	<div class="subnav">
		<div class="nav_content">
			<ul>
				<li class="per"><a href="javascript:void(0);"
					onclick="p_click(0);getAddrData();">发送消息</a></li>
				<li class="per"><a href="javascript:void(0);"
					onclick="p_click(1);getQuartzData();">定时任务</a></li>
				<li class="per"><a href="javascript:void(0);"
					onclick="p_click(2);getSendHistory();">历史记录</a></li>
			</ul>
		</div>
		<script type="text/javascript">
			function getAddrData() {
				$
						.ajax({
							type : "POST",
							url : "${pageContext.request.contextPath}/data/getAddress",
							datatype : "json",
							success : function(data) {
								if (data.success) {
									showSelectAddr(data.addressDataList);
								}
							},
							error : function() {
								alert('操作异常');
								window.location.href = '${pageContext.request.contextPath}/redirect/login';
								return;
							}
						});
			}
			function getQuartzData() {
				$.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/data/getQuartz",
					datatype : "json",
					success : function(data) {
						if (data.success) {
							showQuartzList(data.dataList);
						}
					},
					error : function() {
						alert('操作异常');
						return;
					}
				});
			}
		</script>
	</div>
	<!--消息发送部分开始-->
	<div class="sub_content per_sub_content">
		<div class="message_sending">
			<div class="top">
				<div class="addressee">
					<a href="javascript:void(0);" onclick="showAddress();" title="收件人">选择收件人</a>
					<script type="text/javascript">
						getAddrData();
					</script>
				</div>
				<div class="send">
					<a id="send" href="javascript:void(0);" onclick="send();"
						title="发送">发送</a> <a id="sendAsQuartz" style="display: none"
						href="javascript:void(0);" onclick="addQuartz();" title="定时发送">定时发送</a>
				</div>
				<div id="selectedAddr" class="receive"></div>
				<input id="hideData" type="hidden" value="" />
			</div>
			<div class="middle">
				<div class="quartz">
					<input type="checkbox" id="quartzBox"
						onclick="setQuartz(this,'quartz');" />设置为定时任务
				</div>
				<div class="select-time" id="quartz">
					距发送时间：<select id="sendHour">
					</select>小时 <select id="sendMinute">
					</select>分钟
					<script type="text/javascript">
						var hour = document.getElementById('sendHour');
						var minute = document.getElementById('sendMinute');
						for (var i = 0; i < 48; i++) {
							hour[i] = new Option(i, i);
						}
						for (var i = 0; i < 60; i++) {
							minute[i] = new Option(i, i);
						}
					</script>
				</div>
			</div>
			<div class="edit">
				<div>
					<textarea id="content" name="ta" cols="54" rows="7"
						placeholder="请编辑短信内容..." style="margin-top:10px; font-size:20px;"></textarea>
					<br>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			function showAddress() {
				document.getElementById('pe').style.display = 'block';
				document.getElementById('selectAddr').style.display = 'block';
			}
		</script>
	</div>
	<!--消息发送部分结束-->
	<!--定时任务部分开始-->
	<div class="sub_content per_sub_content" style="display:none">
		<div class="p_quartz">
			<table id="quartzTable"
				style="width: 90%; text-align: center; font-family:楷体;">
				<tr>
					<td>暂无数据</td>
				</tr>
			</table>
		</div>
	</div>
	<!--定时任务部分结束-->
	<!--历史记录部分开始-->
	<div class="sub_content per_sub_content" style="display:none">
		<div class="p_history">
			<ul id="send_his_li">
			</ul>
		</div>
		<script type="text/javascript">
			function getSendHistory() {
				$
						.ajax({
							type : "POST",
							url : "${pageContext.request.contextPath}/historySend/getAll",
							datatype : "json",
							success : function(data) {
								if (data.success) {
									var html = '';
									var list = data.list;
									for (var i = 0; i < list.length; i++) {
										html += '<li><a title="'
												+ list[i].sendContent
												+ '">'
												+ formatDate(list[i].createTime)
												+ ' '
												+ list[i].content
												+ '</a></li>';
									}
									document.getElementById('send_his_li').innerHTML = html;
								} else {
									alert('操作异常');
								}
							},
							error : function() {
								alert('操作异常');
								window.location.href = '${pageContext.request.contextPath}/redirect/login';
								return;
							}
						});
			}
		</script>
	</div>
	<!--历史记录部分结束-->
	<div class="clear"></div>
</div>
<div id="pe" class="delete_alert_bc"></div>
<div id="selectAddr" class="select-addr">
	<div class="top">
		<button onclick="selectAll();">全选</button>
		<button onclick="deSelectAll();">清空</button>
	</div>
	<div id="addrList" class="middle"></div>
	<div class="bottom">
		<button onclick="closeWindow('pe','selectAddr');">取消</button>
		<button onclick="back();">确定</button>
	</div>
	<script type="text/javascript">
		function showSelectAddr(list) {
			document.getElementById('selectedAddr').innerHTML = '';
			document.getElementById('hideData').value = '';
			var addr = document.getElementById('addrList');
			var pul = document.createElement('ul');
			addr.appendChild(pul);
			addr.innerHTML = '';
			for (var i = 0; i < list.length; i++) {
				var pli = document.createElement('li');
				var data = list[i].addressList;

				var img = document.createElement('img');
				img.src = '${pageContext.request.contextPath}/img/coin.gif';
				img.width = '14';
				img.height = '14';

				var a = document.createElement('a');
				a.href = 'javascript:void(0)';
				a.setAttribute('onclick', 'changeList(this,"' + list[i].pkId
						+ '")');
				a.appendChild(img);

				var span = document.createElement('span');
				span.innerHTML = '&nbsp;&nbsp;' + list[i].groupName + ' '
						+ data.length;
				a.appendChild(span);
				a.style.float = 'left';

				var checkBox = document.createElement('input');
				checkBox.type = 'checkbox';
				checkBox.setAttribute('onclick', 'clickGroup(this,"'
						+ list[i].pkId + '")');
				checkBox.style.margin = '2px';
				checkBox.style.float = 'left';

				pli.appendChild(checkBox);
				pli.appendChild(a);
				var br = document.createElement('br');
				pli.appendChild(br);

				var ul = document.createElement('ul');
				ul.setAttribute('id', list[i].pkId);
				ul.style.display = 'none';

				for (var j = 0; j < data.length; j++) {
					var li = document.createElement('li');
					li.style.fontSize = '16px';
					li.style.paddingLeft = '13px';

					var a = document.createElement('a');
					a.href = 'javascript:void(0)';
					a.style.float = 'left';
					a.innerHTML = data[j].name + ' ' + data[j].phoneNumber;

					var checkBox = document.createElement('input');
					checkBox.type = 'checkbox';
					checkBox.value = data[j].pkId;
					checkBox.style.float = 'left';
					checkBox.setAttribute('class', 'selectBox');
					checkBox.style.margin = '2px';

					li.appendChild(checkBox);
					li.appendChild(a);
					var br = document.createElement('br');
					li.appendChild(br);
					ul.appendChild(li);
				}
				pli.appendChild(ul);
				pul.appendChild(pli);
				addr.appendChild(pul);
			}
		}
		function changeList(source, target, type) {
			var e = document.getElementById(target);
			var es = source.getElementsByTagName('img');
			if (type == 1) {
				e.style.display = 'block';
				es[0].src = '${pageContext.request.contextPath}/img/coin2.png';
				return;
			}
			if (type == 0) {
				e.style.display = 'none';
				es[0].src = '${pageContext.request.contextPath}/img/coin.gif';
				return;
			}
			if (e.style.display != 'none') {
				e.style.display = 'none';
				es[0].src = '${pageContext.request.contextPath}/img/coin.gif';
			} else {
				e.style.display = 'block';
				es[0].src = '${pageContext.request.contextPath}/img/coin2.png';
			}
		}
		function clickGroup(source, target) {
			var ul = document.getElementById(target);
			var bs = source.nextSibling;
			var flag = source.checked;
			if (flag) {
				changeList(bs, target, 1);
			} else {
				changeList(bs, target, 0);
			}
			var lis = ul.getElementsByTagName('input');
			for (var i = 0; i < lis.length; i++) {
				lis[i].checked = flag;
			}
		}
		function selectAll() {
			var addr = document.getElementById('addrList')
					.getElementsByTagName('input');
			for (var i = 0; i < addr.length; i++) {
				addr[i].checked = true;
			}
		}
		function deSelectAll() {
			var addr = document.getElementById('addrList')
					.getElementsByTagName('input');
			for (var i = 0; i < addr.length; i++) {
				addr[i].checked = false;
			}
		}
		function back() {
			var addr = document.getElementsByClassName('selectBox');
			var value = '';
			var number = 0;
			for (var i = 0; i < addr.length; i++) {
				if (addr[i].checked) {
					number++;
					value += addr[i].value + ',';
				}
			}
			if (number == 0) {
				alert('未选择收件人');
				return;
			}
			value = value.substring(0, value.length - 1);
			var t = document.getElementById('selectedAddr').innerHTML = '已选择收件人 '
					+ number + ' 人';
			document.getElementById('hideData').value = value;
			closeWindow('pe', 'selectAddr');
		}
		function send() {
			var content = document.getElementById('content').value;
			var ids = document.getElementById('hideData').value;
			if (ids == '') {
				alert('未选择收件人');
				return;
			}
			if (content.trim() == '') {
				alert('未填写发送内容');
				return;
			}
			$
					.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/send/createSend",
						data : {
							'content' : content,
							'sendIds' : ids
						},
						datatype : "json",
						success : function(data) {
							if (data.success) {
								alert('发送成功');
								document.getElementById('content').value = '';
								document.getElementById('selectedAddr').innerHTML = '';
								document.getElementById('hideData').value = '';
								getAddrData();
							} else {
								alert('余额不足');
							}
						},
						error : function() {
							alert('操作异常');
							window.location.href = '${pageContext.request.contextPath}/redirect/login';
							return;
						}
					});
		}
		function addQuartz() {
			var content = document.getElementById('content').value;
			var ids = document.getElementById('hideData').value;
			var hour = document.getElementById('sendHour').value;
			var minute = document.getElementById('sendMinute').value;
			if (ids == '') {
				alert('未选择收件人');
				return;
			}
			if (content.trim() == '') {
				alert('未填写发送内容');
				return;
			}
			$
					.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/send/quartzSend",
						data : {
							'content' : content,
							'sendIds' : ids,
							'hour' : hour,
							'minute' : minute
						},
						datatype : "json",
						success : function(data) {
							if (data.success) {
								alert('添加成功');
								document.getElementById('content').value = '';
								document.getElementById('selectedAddr').innerHTML = '';
								document.getElementById('hideData').value = '';
								var quartz = document
										.getElementById('quartzBox');
								quartz.checked = false;
								setQuartz(quartz);
								getAddrData();
							} else {
								alert('余额不足');
							}
						},
						error : function() {
							alert('操作异常');
							window.location.href = '${pageContext.request.contextPath}/redirect/login';
							return;
						}
					});
		}
		function setQuartz(source) {
			var q = document.getElementById('quartz');
			if (source.checked) {
				document.getElementById('send').style.display = 'none';
				document.getElementById('sendAsQuartz').style.display = 'block';
				q.style.display = 'block';
			} else {
				document.getElementById('send').style.display = 'block';
				document.getElementById('sendAsQuartz').style.display = 'none';
				q.style.display = 'none';
			}
		}
		function showQuartzList(list) {
			if (list.length == 0) {
				return;
			}
			var table = document.getElementById('quartzTable');
			table.innerHTML = '';
			var tr = document.createElement('tr');
			var th1 = document.createElement('th');
			var th2 = document.createElement('th');
			var th3 = document.createElement('th');
			var th4 = document.createElement('th');
			th1.style.width = '48%';
			th2.style.width = '13%';
			th3.style.width = '23%';
			th4.style.width = '16%';
			th1.innerHTML = '内容';
			th2.innerHTML = '发送数量';
			th3.innerHTML = '（预）发送时间';
			th4.innerHTML = '发送状态';
			tr.appendChild(th1);
			tr.appendChild(th2);
			tr.appendChild(th3);
			tr.appendChild(th4);
			table.appendChild(tr);
			for (var i = 0; i < list.length; i++) {
				var tr = document.createElement('tr');
				var td1 = document.createElement('td');
				var td2 = document.createElement('td');
				var td3 = document.createElement('td');
				var td4 = document.createElement('td');
				td1.innerHTML = list[i].content;
				td2.innerHTML = list[i].sendNumber;
				td3.innerHTML = formatDate(list[i].sendTime);
				if (list[i].sendState == 0) {
					td4.innerHTML = '未发送<a href="javascript:void(0);" onclick="cancelQuartz(\''
							+ list[i].pkId
							+ '\')" style="display: inline;color: blue;text-decoration: underline;">取消</a>';
				}
				if (list[i].sendState == 1) {
					td4.innerHTML = '已发送';
				}
				if (list[i].sendState == 2) {
					td4.innerHTML = '发送失败';
				}
				if (list[i].sendState == 3) {
					td4.innerHTML = '已取消';
				}
				tr.appendChild(td1);
				tr.appendChild(td2);
				tr.appendChild(td3);
				tr.appendChild(td4);
				if (list[i].sendState == 0) {
					var td5 = document.createElement('td');
					td5.style.width = '13%';
					var button = document.createElement('button');
					button.setAttribute('onclick', 'cancelQuartz("'
							+ list[i].pkId + '")');
					td5.appendChild(button);
					tr.appendChild(td5);
				}
				table.appendChild(tr);
			}
		}
		function cancelQuartz(sendId) {
			$
					.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/send/cancelQuartz",
						data : {
							'sendId' : sendId
						},
						datatype : "json",
						success : function(data) {
							if (data.success) {
								getQuartzData();
							}
						},
						error : function() {
							alert('操作异常');
							window.location.href = '${pageContext.request.contextPath}/redirect/login';
							return;
						}
					});
		}
	</script>
</div>